package com.cardone.platform.authority.dao;

import java.util.List;
import java.util.UUID;

import lombok.Getter;
import lombok.Setter;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import com.cardone.common.dto.PaginationDto;
import com.cardone.common.util.EntityUtils;
import com.cardone.context.Attributes;
import com.cardone.context.ContextHolder;
import com.cardone.persistent.builder.Model;
import com.cardone.persistent.builder.ModelArgs;
import com.cardone.persistent.builder.ModelUtils;
import com.cardone.persistent.support.JdbcTemplateSupport;
import com.cardone.persistent.support.PaginationArgs;
import com.cardone.platform.authority.dto.UsGrBusinessAreaDto;

/**
 * 用户组与业务范围
 *
 * @author yaohaitao
 *
 */
@Getter
@Setter
public class UsGrBusinessAreaJdbcDao implements UsGrBusinessAreaDao {
	@Override
	public int deleteByIds(final UsGrBusinessAreaDto deleteUsGrBusinessArea) {
		final Model model = ModelUtils.newModel();

		if (MapUtils.isNotEmpty(deleteUsGrBusinessArea.getAttrs())) {
			model.putAll(deleteUsGrBusinessArea.getAttrs());
		}

		model.put(Attributes.ids.name(), deleteUsGrBusinessArea.getIds());

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.deleteByIds.id(), model);
	}

	@Override
	public <P> P findByCodeOrId(final Class<P> mappedClass, final UsGrBusinessAreaDto findUsGrBusinessArea) {
		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(findUsGrBusinessArea, Attributes.code.name());

		final Model model = ModelUtils.newModel(new ModelArgs(findUsGrBusinessArea, whereAndEqProperties));

		if (MapUtils.isNotEmpty(findUsGrBusinessArea.getAttrs())) {
			model.putAll(findUsGrBusinessArea.getAttrs());
		}

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).find(mappedClass, SqlIds.findByCode.id(), model);
	}

	@Override
	public <P> P findById(final Class<P> mappedClass, final UsGrBusinessAreaDto findUsGrBusinessArea) {
		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(findUsGrBusinessArea);

		final Model model = ModelUtils.newModel(new ModelArgs(findUsGrBusinessArea, whereAndEqProperties).setIsSimple(true));

		if (MapUtils.isNotEmpty(findUsGrBusinessArea.getAttrs())) {
			model.putAll(findUsGrBusinessArea.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).find(mappedClass, SqlIds.findById.id(), model);
	}

	@Override
	public <P> List<P> findListByLikeCode(final Class<P> mappedClass, final UsGrBusinessAreaDto findListUsGrBusinessArea) {
		final Model model = ModelUtils.newModel(new ModelArgs(findListUsGrBusinessArea).setIsSimple(true));

		if (MapUtils.isNotEmpty(findListUsGrBusinessArea.getAttrs())) {
			model.putAll(findListUsGrBusinessArea.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).findList(mappedClass, SqlIds.findByLikeCode.id(), model);
	}

	@Override
	public int insertByCode(final UsGrBusinessAreaDto insertUsGrBusinessArea) {
		if (StringUtils.isBlank(insertUsGrBusinessArea.getId())) {
			insertUsGrBusinessArea.setId(UUID.randomUUID().toString());
		}

		final Model model = ModelUtils.newModel(new ModelArgs(Model.Keys.insert.stringValue(), insertUsGrBusinessArea));

		if (MapUtils.isNotEmpty(insertUsGrBusinessArea.getAttrs())) {
			model.putAll(insertUsGrBusinessArea.getAttrs());
		}

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.insertByCode.id(), model);
	}

	@Override
	public <P> PaginationDto<P> paginationByLikeCode(final Class<P> mappedClass, final UsGrBusinessAreaDto paginationUsGrBusinessArea) {
		final Model model = ModelUtils.newModel(new ModelArgs(paginationUsGrBusinessArea).setIsSimple(true));

		if (MapUtils.isNotEmpty(paginationUsGrBusinessArea.getAttrs())) {
			model.putAll(paginationUsGrBusinessArea.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).pagination(mappedClass, new PaginationArgs(SqlIds.readByLikeCode.id(), SqlIds.findByLikeCode.id(), paginationUsGrBusinessArea), model);
	}

	@Override
	public Integer readByCodeNqIdForCount(final UsGrBusinessAreaDto readUsGrBusinessArea) {
		final String[] whereAndEqProperties = { Attributes.code.name() };

		final Model model = ModelUtils.newModel(new ModelArgs(readUsGrBusinessArea, whereAndEqProperties));

		if (MapUtils.isNotEmpty(readUsGrBusinessArea.getAttrs())) {
			model.putAll(readUsGrBusinessArea.getAttrs());
		}

		final String[] whereAndNqProperties = EntityUtils.getWhereProperties(readUsGrBusinessArea);

		if (ArrayUtils.isNotEmpty(whereAndNqProperties)) {
			ModelUtils.put(model, new ModelArgs(Model.Keys.whereAndNq.stringValue(), readUsGrBusinessArea, whereAndNqProperties));
		}

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).read(Integer.class, SqlIds.readByCode.id(), model);
	}

	@Override
	public <P> P saveByIdOrCode(final Class<P> mappedClass, final UsGrBusinessAreaDto saveUsGrBusinessArea) {
		final P oldUsGrBusinessArea = this.findByCodeOrId(mappedClass, saveUsGrBusinessArea);

		if (oldUsGrBusinessArea == null) {
			this.insertByCode(saveUsGrBusinessArea);

			return this.findByCodeOrId(mappedClass, saveUsGrBusinessArea);
		}

		final int updateCount = this.updateByCode(saveUsGrBusinessArea, oldUsGrBusinessArea);

		if (updateCount < 1) {
			return oldUsGrBusinessArea;
		}

		return this.findByCodeOrId(mappedClass, saveUsGrBusinessArea);
	}

	@Override
	public int updateByCode(final UsGrBusinessAreaDto updateUsGrBusinessArea) {
		final UsGrBusinessAreaDto oldUsGrBusinessArea = this.findByCodeOrId(UsGrBusinessAreaDto.class, updateUsGrBusinessArea);

		return this.updateByCode(updateUsGrBusinessArea, oldUsGrBusinessArea);
	}

	private <P> int updateByCode(final UsGrBusinessAreaDto updateUsGrBusinessArea, final P oldUsGrBusinessArea) {
		if (oldUsGrBusinessArea == null) {
			return 0;
		}

		final String[] useProperties = EntityUtils.addNotNullTimeSegmentProperties(new String[] { Attributes.parentId.name(), Attributes.typeId.name(), Attributes.code.name(), Attributes.name.name(), Attributes.value.name(), Attributes.remark.name() }, updateUsGrBusinessArea);

		final String[] updateProperties = com.cardone.common.util.BeanUtils.diffProperties(updateUsGrBusinessArea, oldUsGrBusinessArea, useProperties);

		if (ArrayUtils.isEmpty(updateProperties)) {
			return 0;
		}

		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(oldUsGrBusinessArea, Attributes.id.name());

		com.cardone.common.util.BeanUtils.copyProperties(oldUsGrBusinessArea, updateUsGrBusinessArea, whereAndEqProperties);

		final Model model = ModelUtils.newModel(new ModelArgs(Model.Keys.update.stringValue(), updateUsGrBusinessArea, updateProperties), new ModelArgs(updateUsGrBusinessArea, whereAndEqProperties));

		if (MapUtils.isNotEmpty(updateUsGrBusinessArea.getAttrs())) {
			model.putAll(updateUsGrBusinessArea.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.updateByCode.id(), model);
	}
}